﻿package com.tomvian.twodee.sideon.actions 
{
	import com.tomvian.twodee.common.actions.Action;
	import com.tomvian.twodee.common.Actor;
	import com.tomvian.twodee.common.interactions.Interaction;
	
	/**
	 * Flying
	 * @author Thomas Vian
	 */
	public class Flying extends Action
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public static const DISPLAY_FLYING:String = "flying";
		
		public var ground:Interaction;
		
		public var flyingSpeed:Number;
		public var turningSpeed:Number;
		
		protected var _angle:Number;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Flying(	actor:Actor, 
								ground:Interaction,
								flyingSpeed:Number,
								turningSpeed:Number) 
		{
			super(actor);
			
			this.ground = ground;
			
			this.flyingSpeed = flyingSpeed;
			this.turningSpeed = turningSpeed;
			
			_angle = 0;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function check(state:uint):void 
		{
			if (actorController.keyPress.jump)
			{
				actor.currentAction = this;
			}
		}
		
		override public function start():void 
		{
			_angle = 0;
			ground.interacting = false;
		}
		
		override public function act():void 
		{
			if (enabled)
			{
				if (!ground.interacting)
				{
					if (actorController.left) _angle -= turningSpeed;
					if (actorController.right) _angle += turningSpeed;
					
					actorMass.velocity.setFromAngle(_angle, flyingSpeed);
				}
				else
				{
					actor.returnToDefault();
				}
			}
		}
		
		override public function display():void 
		{
			actorState.state = DISPLAY_FLYING;
		}
	}
}